home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: A Very Simple Socket Question
- Date: 24 Jan 1996 08:29:03 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4e5mofINNfjh@keats.ugrad.cs.ubc.ca>
- References: <4dfgj8$6p9@nntp.ucs.ubc.ca> <4doprj$9t8@spanky.pls.ov.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4doprj$9t8@spanky.pls.ov.com>,
- Fletcher.Glenn@ov.com <glenn@ov.com> wrote:
- >In article 6p9@nntp.ucs.ubc.ca, evil@unixg.ubc.ca (Peter Pan) writes:
- >>Please help! I am a C beginner.
- >>
- >>======================================
- >>Client:
- >>
- >>int sd, addrlen;
- >>struct sockaddr_in svaddr;
- >>
- >>char *data;
- >>data = "abcdef";
- >>
- >>..... /* skip */
- >>
- >>sendto(sd, data, strlen(data), 0, &svaddr, &addrlen);
- > ^^^^^^^^^^^^
- >This will not send the terminating null character, use "strlen(data) + 1".
-
- Or sizeof(data), in this case. That avoid searching for a zero.
-
- >>data = (char*) malloc( 100*sizeof(char) );
- >>
- >>recvfrom(sd2, data, sizeof(data), 0, &claddr, &addrlen);
- > ^^^^^^^^^^^^
- >data is a char * with a size of 4. What you really want is 100.
- >Even so, I expect you will block waiting for the remaining 93 chars.
-
- Not true. The recvfrom() will exit as soon as it gets a datagram of any
- length. That's what the (here ignored) return value is for; to tell you how
- much has been read. A lot of devices under UNIX work this way. The request size
- of a read() or recv() is only a hint at how much you can take. The return value
- tells you how much you got. This is needed even for ordinary files for the
- occasion when you ask for more bytes than are remaining in the file.
- --
-
-